package com.bwie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbGoodsMapper;
import com.bwie.mapper.TbOrderMapper;
import com.bwie.mapper.TbOrderimgMapper;
import com.bwie.pojo.TbGoods;
import com.bwie.pojo.TbOrder;
import com.bwie.pojo.TbOrderimg;
import com.bwie.service.TbOrderService;
import com.bwie.utils.R;
import com.bwie.vo.TbOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder> implements TbOrderService {

    @Autowired
    TbOrderMapper tbOrderMapper;
    @Autowired
    TbGoodsMapper tbGoodsMapper;

    @Autowired
    TbOrderimgMapper tbOrderimgMapper;

    /**
     * @description: 钱够生成订单
     * @author: xushuaihang
     * @Date: 2024/6/17 13:41
     * @param:
     * @param tbOrderVo
     * @return: com.bwie.utils.R
    **/
    @Override
    public R addOrder(TbOrderVo tbOrderVo) {

        TbOrder tbOrder = BeanUtil.toBean(tbOrderVo, TbOrder.class);

        TbGoods tbGoods = tbGoodsMapper.selectById(tbOrderVo.getGoodsId());

        tbOrder.setGoodsTitle(tbGoods.getGoodsTitle());
        tbOrder.setGoodsPrice(tbGoods.getGoodsPrice());

        tbOrder.setGoodsSort(tbGoods.getGoodsSort());

        tbOrderMapper.insert(tbOrder);

        for (TbOrderimg tbOrderimg : tbOrderVo.getTbOrderimgs()) {
            tbOrderimg.setOrderId(tbOrder.getId());

            tbOrderimgMapper.insert(tbOrderimg);

        }

        return R.ok().message("抢购成功！");
    }

    /**
     * @description: 订单列表查询
     * @author: xushuaihang
     * @Date: d 13:59
     * @param:
     * @return: com.bwie.utils.R
    **/
    @Override
    public R orderList() {

        List<TbOrder> list = tbOrderMapper.orderList();


        return R.ok().data("list",list);
    }
}
